<?php
namespace app\index\controller;
use app\index\controller\Commonse;
use think\Db;
use think\Request;
use Think\Session;
class User extends Commonse {
    private $user;
    public function __construct(){
        parent::__construct();
        
    }

    public function user(){
        if(Request::instance()->isPost()){
            $chongz=input('post.chongz');
            $uid=input('post.uid',0,'intval');
            if(!is_numeric($chongz)&&$chongz>0){
                $this->error('输入内容不合法');
            }
            $data=array(
                'aid'=>Session::get('adminid'),
                'money'=>$chongz,
                'yid'=>$uid,
                'add_time'=>time()
            );
            if(db('Users')->where('id='.$uid)->setInc('user_money',$chongz)){
                $redisUser = redisd() -> HGet("user", $uid);
                $redisUser['user_money'] = $redisUser['user_money'] + $chongz;
                redisd() -> HSet("user", $uid, $redisUser);
                db('Czh')->insert($data);
                $czinfo = array(
                    "user_id" => $uid,
                    "money"   => $chongz,
                    "add_time"=> time(),
                    "status"  => 1
                );
                db('recharge') -> insert($czinfo);
                $this->success('充值成功');
            }else{
                $this->error('充值失败');
            }
        }
        $keyword = input('get.keyword','');
        if($keyword){
            $where = "id={$keyword}";
        }else{
          $where = "";
        }
        $list = Db::name('users')->where($where)->where('user_type=0')->order('id desc')->paginate(50);
        $this->assign("list", $list);
        $page = $list->render();
       return $this->fetch();
    }
    function mima(){//重置密码 6个0
        $id=input('uid');
        $data=array(
            'pwd'=>md5('000000')
        );
       if(Db::name('users')->where('id='.$id)->update($data)){
           $this->success('重置成功');
       } else{
           $this->error('重置失败');
       }
    }
    function jianqian(){//减钱
        $chongz=input('post.chongz');
        $uid=input('post.uid',0,'intval');
        if(!is_numeric($chongz)&&$chongz>0){
            $this->error('输入内容不合法');
        }
        $data=array(
            'aid'=>Session::get('adminid'),
            'money'=>$chongz,
            'yid'=>$uid,
            'add_time'=>time()
        );
        if(db('Users')->where('id='.$uid)->setDec('user_money',$chongz)){
            $redisUser = redisd() -> HGet("user", $uid);
            $redisUser['user_money'] = $redisUser['user_money'] - $chongz;
            redisd() -> HSet("user", $uid, $redisUser);
            db('Czh')->insert($data);
            $this->success('操作成功');
        }else{
            $this->error('操作失败');
        }
    }
    function congzhi(){
        $keyword = input('get.keyword','');
        if($keyword){
            $where = "yid={$keyword}";
        }else{
            $where = "";
        }
        $list=db('Czh')->where($where)->order('id desc')->paginate(50);
        $this->assign("list", $list);
        return $this->fetch();
    }
    function one(){//一级分销
        $id=input('id',0,'intval');//获取用户id
        $se=input('se',0);
        switch ($se){
            case 1:{//一级分销
                $user_id="first_parent=";
            };break;
            case 2:{//二级分销
                $user_id = "second_parent=";
            };break;
            case 3:{//三级分销
                $user_id = "three_parent=";
            };break;
           
           
        }
        $where = "$user_id".$id;
        $Mode=db('users');
        $list=$Mode->where($where)->order('id asc')->paginate(50);

         $this->assign("list", $list);
        $page = $list->render();
        $this->assign('info',$se);
      return  $this->fetch();
    }

    
    public function state(){
        if(Request::instance()->isGet()){
           $id=input('id');
           $info=Db::name('users')->where('id='.$id)->find();
           $this->assign("info",$info);
           return $this->fetch();
        }else{
            $id=input('post.id',0,'intval');
            $zc=input('post.zc',0,'intval');
            $data=array(
                'state'=>$zc
            );
            if(Db::name('users')->where('id='.$id)->update($data)){
                $redisUser = redisd() -> HGet("user", $id);
                $redisUser['state'] = $zc;
                redisd() -> HSet("user", $id, $redisUser);
                $this->success('修改成功',Url('User/user')); 
            }else{
                $this->error('修改失败');
            }
        }
        
    }

    public function quanx(){
        if(Request::instance()->isGet()){
            $id=input('id');
            $info=Db::name('users')->where('id='.$id)->find();
            $this->assign("info",$info);
            return $this->fetch();
        }else{
            $id=input('post.id',0,'intval');
            $zc=input('post.zc',0,'intval');
            $data=array(
                'is_create'=>$zc,
                'daili'=>$zc
            );
            if(Db::name('users')->where('id='.$id)->update($data)){
                $redisUser = redisd() -> HGet("user", $id);
                $redisUser['is_create'] = $zc;
                $redisUser['daili'] = $zc;
                redisd() -> HSet("user", $id, $redisUser);
                $this->success('修改成功',Url('User/user'));
            }else{
                $this->error('修改失败');
            }
        }
    
    }
    
    public function laba(){//喇叭权限
        if(Request::instance()->isGet()){
            $id=input('id');
            $info=Db::name('users')->where('id='.$id)->find();
            $this->assign("info",$info);
            return $this->fetch();
        }else{
            $id=input('post.id',0,'intval');
            $zc=input('post.zc',0,'intval');
            $data=array(
                'is_laba'=>$zc
            );
            if(Db::name('users')->where('id='.$id)->update($data)){
                $redisUser = redisd() -> HGet("user", $id);
                $redisUser['is_laba'] = $zc;
                redisd() -> HSet("user", $id, $redisUser);
                $this->success('修改成功',Url('User/user'));
            }else{
                $this->error('修改失败');
            }
        }
    
    }
    public function robot(){//机器人列表
        
        $list = Db::name('users')->where('user_type=1')->order('id desc')->paginate(50);
        $this->assign("list", $list);
        $page = $list->render();
        return $this->fetch();
    }
    public  function add(){
       
        return $this->fetch();
    }
  public  function update(){
        $id=input('id');
        if(Db::name('users')->where('id='.$id)->delete()){
            redisd() -> HDel("user", $id);
            $this->success('删除成功');
        }else{
            $this->error('删除失败');
        }
    }
    function addSave(){
        if(Request::instance()->isGet()){
            $jiq =input('jiq');
            if(empty($jiq)){
               $this->error('名字不能为空'); 
            }
            $data=array(
                'user_type'=>1,
                'nickname'=>$jiq,
                "reg_time"=>time(),
                "user_money"=>"1000"
                 
            );
            if(Db::name('users')->insert($data)){
                $this->success('添加成功',url('user/robot'));
            }else{
                $this->error('添加失败');
            }
        }
    }
  

   public function xiang(){//用户下注详情
        if(Request::instance()->isGet()){
            $id=input('id');
            $Model =db('order');
           
            $list=$Model->where('user_id='.$id)->order('add_time desc')->paginate(50);
             $this->assign("list", $list);
           $page = $list->render();
           return $this->fetch();
         
        }
       
    }  
}
